home *** CD-ROM | disk | FTP | other *** search
- RCS_ID_C="$Id: fonts.c,v 3.2 1994/02/25 02:33:38 ppessi Exp $";
- /*
- * fonts.c - Amiga font handling routines
- *
- * Authors: Pekka Pessi <Pekka.Pessi@hut.fi>,
- * original from Niftyterm by Chris Newman, Todd Williamson.
- *
- * Copyright (c) 1989, 1993 by authors. All Rights Reserved
- *
- * Permission is granted to copy, modify, and use this as long
- * as this notice remains intact. This is a nifty program.
- *
- * DISCLAIMER: the authors of this program are not responsible
- * for any damage or other problems caused by it.
- *
- * Created : Sat Nov 13 06:55:40 1993 ppessi
- * Last modified: Fri Feb 25 02:09:10 1994 ppessi
- *
- * $Log: fonts.c,v $
- * Revision 3.2 1994/02/25 02:33:38 ppessi
- * Added RCS ID
- *
- * Revision 3.1 1994/01/07 22:51:18 ppessi
- * Version 3 beta
- *
- * Revision 2.3 93/11/24 18:19:58 ppessi
- * Fixed some fontname problems..
- *
- * Revision 2.2 93/11/18 18:35:13 ppessi
- * *** empty log message ***
- *
- * Revision 2.1 93/11/17 15:45:25 ppessi
- * Changed function names to more logical, restructured
- * the role of rastport pointer and fonts.
- *
- * Revision 2.0 93/11/15 03:43:43 ppessi
- * Version 2 initial revision
- *
- */
-
- #include <assert.h>
- #include <string.h>
- #include <stdlib.h>
-
- #include "nifty.h"
- #include "display.h"
- #include "dispmacros.h"
- #include "napsaprefs.h"
-
- #include "amiga.h"
- #include "wimp.h"
-
- #if USE_PRAGMAS
- #include <proto/diskfont.h>
- #endif
- #if USE_INLINE
- #include <inline/diskfont.h>
- #endif
- #if USE_CLIB
- #include <clib/diskfont_protos.h>
- #endif
-
- /* I didn't want to have to type this */
- #define tf_Name tf_Message.mn_Node.ln_Name
-
- /* array of fonts */
- static struct {
- struct TextFont *attr; /* font pointer */
- ULONG style; /* style to use with this font */
- } fonts[ATTRIB_MASK + 1];
-
- /*
- * Font name information
- */
- static struct font_names {
- char *base; /* main font name */
- char *alt; /* alternate character set font (as, ae) */
- char *wide; /* wide font */
- char *top; /* top of double height */
- char *bot; /* bottom of double height */
- char *altwide; /* alternate wide font */
- char *alttop; /* alternate top font */
- char *altbot; /* alternate bottom font */
- char *narrow; /* for 132 column mode */
- } font_names;
-
- #define NO_OF_FONTS (sizeof(font_names)/sizeof(char*))
-
- static char fontpostfixes[] =
- ".font" NUL "v.font" NUL "w.font" NUL "t.font" NUL
- "b.font" NUL "vw.font" NUL "vt.font" NUL "vb.font" NUL
- "n.font" NUL;
-
- /*
- * Load in font #i, return true if succesfull
- */
- static int LoadFont(ULONG i)
- {
- char *fontname;
- int retries = 0, mem = 0, disk = 0;
- struct TextAttr ta;
- struct TextFont *tf = NULL, *tfd = NULL;
-
- i &= ATTRIB_MASK;
- switch (i & (DOUBLE1 | DOUBLE2 | ALTERNATE)) {
- case DOUBLE1 | DOUBLE2:
- fontname = font_names.wide;
- break;
-
- case DOUBLE1 | DOUBLE2 | ALTERNATE:
- fontname = font_names.altwide;
- break;
-
- case DOUBLE1:
- fontname = font_names.top;
- break;
-
- case DOUBLE1 | ALTERNATE:
- fontname = font_names.alttop;
- break;
-
- case DOUBLE2:
- fontname = font_names.bot;
- break;
-
- case DOUBLE2 | ALTERNATE:
- fontname = font_names.altbot;
- break;
-
- case ALTERNATE:
- fontname = font_names.alt;
- break;
-
- default:
- fontname = font_names.base;
- break;
- }
-
- ta.ta_YSize = wm.font.h;
- ta.ta_Style = (i & STYLE_MASK);
- ta.ta_Flags = 0;
-
- do {
- ta.ta_Name = (STRPTR)fontname;
-
- /* Is it already loaded? */
- if (tf = OpenFont(&ta))
- if (!lstrncmp(tf->tf_Name, fontname, strlen(tf->tf_Name)))
- if (tf->tf_YSize == ta.ta_YSize) {
- mem = 1; /* Maybe? */
- if (tf->tf_Style == ta.ta_Style)
- mem = 2; /* Perfect! */
- }
-
- /* Nope, but perhaps it is on disk? */
- if ((mem < 2) && (tfd = OpenDiskFont(&ta)))
- if (!lstrncmp(tfd->tf_Name, fontname, strlen(tfd->tf_Name)))
- if (tfd->tf_YSize == ta.ta_YSize)
- disk = 1;
-
- if (disk || mem) {
- if (disk && mem)
- if (tfd->tf_Style > tf->tf_Style)
- mem = 0;
- /* */
- if (!mem) {
- if (tf) CloseFont(tf);
- tf = tfd;
- } else
- if (tfd) CloseFont(tfd);
-
- fonts[i].style = (~(tf->tf_Style) & ta.ta_Style);
- fonts[i].attr = tf;
- return 1;
- }
-
- if (tf) {
- CloseFont(tf);
- tf = NULL;
- }
-
- if (tfd) {
- CloseFont(tfd);
- tfd = NULL;
- }
-
- if (i & ALTERNATE && retries == 0)
- fontname = font_names.base;
- else if (i & ALTERNATE && retries == 1)
- fontname = DEFAULTFONT "v.font";
- else if (retries == 0)
- fontname = DEFAULTFONT ".font", retries++;
- else {
- fontname = "topaz.font";
- ta.ta_YSize = 8;
- }
- } while (++retries <= 3);
-
- /* Set at least something */
- fonts[i].attr = fonts[0].attr;
-
- /* Give up */
- return 0;
- }
-
- /*
- * Set fontnames
- */
- static void setbasefont(char *name)
- {
- char *c, *t, *posts, **namep;
- long height = 11;
-
- int DEBUGSIZE;
-
- c = FilePart(name);
- if (c == name) {
- height = DEFAULTFONTHEIGHT;
- } else {
- height = DEFAULTFONTHEIGHT;
- StrToLong(c, &height);
- wm.font.h = height;
-
- c = PathPart(name);
- c[0] = '\0';
- }
-
- c = malloc(DEBUGSIZE = strlen(name) * NO_OF_FONTS + sizeof(fontpostfixes));
-
- if (!c) {
- fatalError(MEMORY_ERROR_MSG);
- }
-
- posts = fontpostfixes;
- namep = &font_names.base;
-
- do {
- *namep++ = c;
- /* Copy basename */
- for (t = name; *c = *t; t++, c++)
- ;
- /* concat prefix */
- for (; *c++ = *posts++; t++)
- ;
- } while (*posts);
-
- assert(c <= font_names.base + DEBUGSIZE);
- }
-
- /*
- * Initialize fonts
- */
- void initfonts(void)
- {
- int i;
-
- for (i = 0; i <= ATTRIB_MASK; i++) {
- fonts[i].attr = NULL;
- fonts[i].style = 0;
- }
-
- /* Set the base font */
- setbasefont(np.basefont);
-
- if (LoadFont(0L)) {
- /* Initialize font dimensions for display routines */
- struct TextFont *tf = fonts[0].attr;
-
- wm.font.w = tf->tf_XSize;
- wm.font.h = tf->tf_YSize;
- wm.font.b = tf->tf_Baseline;
- } else {
- /* Something is badly broken */
- fatalError("Cannot load any font");
- }
- }
-
- /*
- * Free loaded fonts
- */
- void deinitfonts(void)
- {
- int i;
-
- for (i = 0; i <= ATTRIB_MASK; i++) {
- if (fonts[i].attr)
- CloseFont(fonts[i].attr);
- fonts[i].attr = NULL;
- }
- }
-
- void setfont(register struct RastPort *rastport, register ULONG style)
- {
- if (!fonts[style].attr)
- LoadFont(style);
-
- SetFont(rastport, fonts[style].attr);
-
- SetSoftStyle(rastport, fonts[style].style, 0xffffffff);
- }
-
-